<?php
include("../lang/cn/company_type_config.php");
include_once("../module/product/includes/plugin_order_class.php");
$order = new order;
include_once("../module/micro_stock/includes/plugin_micro_stock_class.php");
$microStock = new MicroStock();

if($_POST["act"]=='getSellAmountByCond')
{//显示销售额
    $cond = str_replace("\'", "'", $_POST['cond']);
    $salesInfo = $microStock->getSalesInfo($cond);
    $html = "<div style='height:200px;'>总金额:".$config['money'].$salesInfo."</div>";
    die($html);
}

$sql = "select payment_id,payment_name from ".PAYMENT;
$db->query($sql);
$pay = $db->getRows();
$tpl->assign("pay",$pay);

if($_GET['act']=='opear' && $_GET['type'] && $_GET['orderId']>0){//操作订单
    if($_GET['type']=='pay'){
        $sql = "select order_id,order_time,total_price from ".MICROSTOCKORDER." where order_id={$_GET['orderId']}";
        $db->query($sql);
        $de = $db->fetchRow();
        $tpl->assign('de',$de);
    }
    
    $tpl->assign('type',$_GET['type']);
    $tpl->assign('orderId',$_GET['orderId']);
    $tpl->display('order_opear.htm');
    exit;
}
if($_POST['type'] && $_POST['orderId']>0){
    switch ($_POST['type'])
    {
        case 'cancel':
            $microStock->cancelMicroOrder($_POST['orderId'],$_POST['admin_remark']);break;
        case 'pay':
            $payment_name = "";
            $sql = "select payment_name from ".PAYMENT." where payment_id={$_POST['payment_id']}";
            $db->query($sql);
            $payment_name = $db->fetchField('payment_name');
            $data = array(
                'payment_id'=>$_POST['payment_id'],
                'payment_name'=>$payment_name,
                'admin_remark'=>'后台支付，操作人'.$_SESSION['ADMIN_USER']
            );
             
            $microStock->updateOderStatus($_POST['orderId'], 2, $data);
             
            include_once("../module/payment/includes/payment_class.php");
            $payment = new payment();
            $payment->apply_result($_POST['orderId']);
        default:
            break;
    }
    msg("module.php?m=micro_stock&s=micro_stock_orders.php");
}

if($_GET['operation']=="edit")
{
    if($_POST['act'])
    {
        unset($_GET['operation']);
        unset($_GET['s']);
        unset($_GET['m']);
        $time=time();
        	
        //修改
        if($_POST["act"]=='cancel' and is_numeric($_POST['id']))
        {
            $admin_remark=$_POST['other_reason']?$_POST['admin_remark']." ".$_POST['other_reason']:$_POST['admin_remark'];

            $sql = "select order_id from ".MICROSTOCKORDER." where id='{$_POST['id']}'";
            $db->query($sql);
            $order_id = $db->fetchField('order_id');
            //取消
            $microStock->cancelMicroOrder($order_id,$admin_remark);

            unset($_GET['editid']);
        }
        $getstr=implode('&',convert($_GET));
        msg("?m=huopin&s=order.php{$getstr}");
    }
    //信息
    if($_GET['editid'] and is_numeric($_GET['editid']))
    {
        $sql="select * from ".MICROSTOCKORDER." where id='$_GET[editid]'";
        $db->query($sql);
        $de=$db->fetchRow();
        	
        //商品信息
        $productList = array();
        if(!empty($de)){
            $sql = "select * from ".MICROSTOCKORDERPRO." where order_id={$de['order_id']}";
            $db->query($sql);
            $productList = $db->getRows();
        }
        $tpl->assign('productList',$productList);
        	
        // 			$sql="select * from ".FASTMAIL."  order by id";
        // 			$db->query($sql);
        // 			$re=$db->getRows();
        // 			$tpl->assign("re",$re);
        	
//         $tpl->assign("district",GetDistrict());
        $tpl->assign("config",$config);
    }
}else{

    $field = "";
    $cond = "";
    
    //----------------------分仓
    $general_shop =$order->get_general_shop();
    
    if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
        msg('noright.php');
        exit;
    }
    if ($general_shop){
        $_GET['stock_shop'] =$general_shop;
    }
    if ($_GET['stock_shop']){
        $cond.=" and stock_shop = '$_GET[stock_shop]' ";
    }
    //-----------------
    
    if(is_numeric($_GET['type'])){
        $cond .= " and status={$_GET['type']}";
    }else{
        $cond .= " and status>=0";
    }
    if(is_numeric($_GET['status'])){
        if($_GET['status']==2)
        {
            $cond .= " and status>=2";
        }else{
            $cond .= " and status={$_GET['status']}";
        }
    }
    if($_GET['code']){
        $cond .= " and order_id='{$_GET['code']}'";
    }
    if($_GET['order_pay_status'])
    {
        $cond.=" and pay_status = '$_GET[order_pay_status]' ";
    }
    if($_GET['pay'])
    {
        $cond.=" and payment_id = '$_GET[pay]' ";
    }
    if($_GET['maxp'])
    {
        $cond.=" and total_price <= '$_GET[maxp]' ";
    }
    if($_GET['minp'])
    {
        $cond.=" and total_price >= '$_GET[minp]' ";
    }
    if($_GET['stime'])
    {
        $cond.=" and IFNULL(order_time,0) > '".date('Y-m-d',strtotime($_GET[stime]))."' ";
    }
    if($_GET['etime'])
    {
        $cond.=" and IFNULL(order_time,0) < '".date('Y-m-d',strtotime($_GET[etime]))." 23:59:59' ";
    }
    if($_GET['p_stime'])
    {
        $cond.=" and IFNULL(pay_time,0) > '".date('Y-m-d',strtotime($_GET[p_stime]))."' ";
    }
    if($_GET['p_etime'])
    {
        $cond.=" and IFNULL(pay_time,0) < '".date('Y-m-d',strtotime($_GET[p_etime]))." 23:59:59' ";
    }
    if($_GET['user'])
    {
        $sqluser="select userid from ".MEMBER." where  username like '%$_GET[user]%' or name like '%$_GET[user]%' limit 1";
        $db->query($sqluser);
        $userid=$db->fetchField('userid');
        $cond.=" and member_id='$userid'";
    }
    if($_GET['userid']){
        $cond.=" and member_id='$_GET[userid]'";
    }
    if($_GET['onlytoday']){
        $_GET['order_source']=1;//只取来源为微仓的订单
        $timeType = $_GET['onlytoday'];
        $cond .= " and status>1";
        switch ($timeType)
        {
            case 1://今天
                $time = date('Y-m-d');
                $cond .= " and order_time>='{$time}'";
                break;
            case 2://昨天
                $time = date('Y-m-d',strtotime('-1 day'));
                $rtime = $time." 23:59:59";
                $cond .= " and order_time>='{$time}' and order_time<='{$rtime}'";
                break;
            case 3://本月
                $time = date('Y-m');
                $cond .= " and order_time>='{$time}'";
                break;
            default://全部
                break;
        }
    }
    if($_GET['order_source']){
        if($_GET['order_source']==1){
            $cond .= " and hp_order_id<=0";
        }elseif($_GET['order_source']==2){
            $cond .= " and hp_order_id>0";
        }
    }
    
    //查询总金额的条件
    $amountCond = "";
    $amountCond = str_replace("'", "\'", $cond);
    $tpl->assign('amountCond',$amountCond);
    
    //=============================
    include_once("../includes/page_utf_class.php");
    $page = new Page;
    $page->listRows=20;
    if (!$page->__get('totalRows')){
        $de['count']=$page->totalRows = $microStock->getOrderListTotalNum($cond);//总数
    }
    $de['count']=$de['count']?$de['count']:($_GET['totalRows']?$_GET['totalRows']:'0');
    $de['page'] = $page->prompt();
    $de['perPageRows'] = $page->listRows;
    //=============================
    
    //列表数据
    $de['list'] = $microStock->getOrderList($field,$cond,'create_time desc',$page->firstRow,$page->listRows);
    foreach ($de['list'] as $k=>$v)
    {
        $de['list'][$k]['client_platform_str'] = $client_platform[$v['client_platform']];
        $de['list'][$k]['client_system_str'] = $client_platform[$v['client_system']];
        
        $sql = "select username,name from ".MEMBER." where userid={$v['member_id']} limit 1";
        $db->query($sql);
        $memberInfo = $db->fetchRow();
        $de['list'][$k]['username'] = $memberInfo['username'];
        $de['list'][$k]['nick_name'] = $memberInfo['name'];
    }

}
$statu_list =array('0'=>'已取消','1'=>'未支付','2'=>'已支付');

$stock_shops=$order->get_stock_shops();
$tpl->assign("stock_shops",$stock_shops);

$tpl->assign("statu_list",$statu_list);
$tpl->assign('de',$de);
$tpl->display('micro_stock_orders.htm');